package jp.co.jmas.jmasvideo.jaxb;

import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class AclUtil {
	public static String marshal(AccessControlList acl) {
		try {
			StringWriter writer = new StringWriter();
			JAXBContext jaxbContext = JAXBContext.newInstance(AccessControlList.class);
			Marshaller marshaller = jaxbContext.createMarshaller();

			// output pretty printed
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			marshaller.marshal(acl, writer);
			return writer.toString();

		} catch (JAXBException e) {
			// TODO

			e.printStackTrace();
			return null;
		}
	}

	public static AccessControlList unmarshal(InputStream is) {
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(AccessControlList.class);
			Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
			return (AccessControlList) unmarshaller.unmarshal(is);

		} catch (JAXBException e) {
			// TODO
			e.printStackTrace();
			return null;
		}
	}

	public static AccessControlList unmarshal(String xml) {
		StringReader reader = new StringReader(xml);
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(AccessControlList.class);
			Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
			return (AccessControlList) unmarshaller.unmarshal(reader);

		} catch (JAXBException e) {
			// TODO
			e.printStackTrace();
			return null;
		}
	}
}
